import 'package:flutter/material.dart';
import 'package:gd_assistant/api/apiHandler.dart';
import 'package:gd_assistant/model/orderItemModel.dart';
import 'package:gd_assistant/page/public/colors.dart';
import 'package:gd_assistant/page/public/themeUtils.dart';

class selectArchivesPage extends StatefulWidget {
  selectArchivesPage({Key key, 
                      this.selectTags,
                      this.selectedEnd}) : super(key : key);
  List<String> selectTags = [];
  Function selectedEnd = (){};
  @override
  _selectArchivesPageState createState() => _selectArchivesPageState();
}

class _selectArchivesPageState extends State<selectArchivesPage> {
  bool isDark = false;
  List<OrderItem> items = [];
  int selectedIndex = -1;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async{
   items = await apiHandler.getOrderItems();
   for (OrderItem item in items) {
     for (Items tags in item.items) {
        for (String selectTags in (widget.selectTags != null ? widget.selectTags : [])) {
          if (tags.name == selectTags) {
            tags.isSelected = true;
          }
       }
     }
   }
   setState((){
     selectedIndex = 0;
   });
  }

   Widget getRow(int i) {
    return new GestureDetector(
      child: new Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
        color: i == selectedIndex ? isDark ? Colours.dark_text_gray : Colors.white : isDark ? Colours.dark_bg_color : Colors.grey[200],
        child: new Text(items[i].name,
            style: TextStyle( color: i == selectedIndex ? isDark ? Colors.white : Colors.black : isDark ? Colours.dark_text : Colors.black, fontSize: 16)),
      ),
      onTap: () {
        setState(() {
          selectedIndex = i;
        });
      },
    );
  }

   Widget getChip() {
    return Wrap(
        spacing: 10.0, //两个widget之间横向的间隔
        direction: Axis.horizontal, //方向
        alignment: WrapAlignment.start, //内容排序方式
        children: List<Widget>.generate(
          selectedIndex < 0 ? 0 : items[selectedIndex].items.length,
          (int index) {
            return GestureDetector(
              onTap: () {
                setState(() {
                  items[selectedIndex].items[index].isSelected = !items[selectedIndex].items[index].isSelected;
                });
              },
              child: Column(
                children: <Widget>[
                  new Container(
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 20, horizontal: 12),
                    color: isDark ? Colours.dark_bg_gray : Colors.white,
                    child:
                      Row(
                        children: <Widget>[
                          Expanded(
                            flex: 1,
                            child: new Text(items[selectedIndex].items[index].name,
                                style: TextStyle( color: isDark ? Colours.dark_text: Colors.black, fontSize: 16)),
                          ),
                          Icon(Icons.check_circle, size: 18, color: items[selectedIndex].items[index].isSelected != null &&  items[selectedIndex].items[index].isSelected ? Colors.blue : Colors.grey[400])
                        ],
                      ),
                    ),
                  Container(margin: EdgeInsets.only(left: 12), height: 0.1, color: isDark ? Colours.dark_bg_gray : Colors.grey[400])
                ],
              ),
            );
          },
        ).toList(),
      );
  }

  @override
  Widget build(BuildContext context) {
    isDark = ThemeUtils.isDark(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("选择项目"),
        elevation: 0
      ),
      body: new Row(
      mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          new Expanded(
              flex: 2,
              child: Container(
                color: isDark ? Colours.dark_bg_color : Colors.grey[100],
                child: new ListView.builder(
                    itemCount: items.length,
                    itemBuilder: (BuildContext context, int position) {
                      return getRow(position);
                    }),
          )),
          new Expanded(
              flex: 5,
              child: Column(
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: ListView(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.topLeft,
                            color: Colors.grey[100],
                            child: getChip(), //传入一级分类下标
                          )
                        ],
                    ),
                  ),
                  Container(
                    height: MediaQuery.of(context).padding.bottom + 60,
                    color: isDark ? Colours.dark_bg_color : Colors.white,
                    child: Row(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                        ),
                        Expanded(
                          flex: 1,
                          child: FlatButton(
                            color: isDark ? Colours.dark_bg_gray : Colors.grey[100],
                            shape: StadiumBorder(side: BorderSide.none),
                            textColor: Colors.blue,
                            child: Text("重置"),
                            onPressed: (){
                              for (OrderItem item in items) {
                                for (Items tags in item.items) {
                                    tags.isSelected = false;
                                }
                              }
                              setState(() {});
                            },
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                        ),
                        Expanded(
                          flex: 1,
                          child: FlatButton(
                            color: Colors.blue,
                            shape: StadiumBorder(side: BorderSide.none),
                            textColor: Colors.white,
                            child: Text("确认"),
                            onPressed: (){
                              List<String> end = [];
                              for (OrderItem item in items) {
                                for (Items tags in item.items) {
                                    if (tags.isSelected) {
                                      end.add(tags.name);
                                    }
                                }
                              }
                              if (widget.selectedEnd != null) {
                                 widget.selectedEnd(end);
                              }
                              Navigator.pop(context);
                            },
                          ),
                        ),
                        Padding(
                          padding: EdgeInsets.only(left: 10),
                        ),
                      ],
                    ),
                  )
                ],
              )
          ),
        ],
      )
    );
  }
}